import Vue from "vue"
import VueRouter from "vue-router"

Vue.use(VueRouter)

import Layout from "@/layout"
import { getToken } from "@/utils/auth"
/**
 * @type {boolean} true | false true 不显示
 * @package '是否在导航栏显示'
 * hidden
 * */
const routes = [
  {
    path: "/login",
    name: "Login",
    hidden: true,
    component: () => import("@/views/Login")
  },
  {
    path: "/",
    component: Layout,
    redirect: "/home",
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("@/views/Home"),
        meta: { title: "首页", icon: "el-icon-s-home" }
      }
    ]
  },
  {
    path: "/user",
    component: Layout,
    meta: { title: "用户管理", icon: "icon-yonghuguanli1" },
    children: [
      {
        path: "/userlist",
        name: "user",
        component: () => import("@/views/User"),
        meta: { title: "用户列表", icon: "icon-yonghuliebiao" }
      },
      {
        path: "/role",
        name: "role",
        component: () => import("@/views/Role"),
        meta: { title: "角色管理", icon: "icon-jiaoseguanli" }
      }
    ]
  },
  {
    path: "/project",
    component: Layout,
    meta: { title: "项目管理", icon: "icon-xiangmuguanli" },
    children: [
      {
        path: "/item",
        name: "item",
        component: () => import("@/views/ProjectItem"),
        meta: { title: "项目列表", icon: "el-icon-coin" }
      },
      {
        path: "/class",
        name: "class",
        component: () => import("@/views/Class"),
        meta: { title: "班级管理", icon: "el-icon-coin" }
      }
    ]
  },
  {
    path: "/student",
    component: Layout,
    children: [
      {
        path: "index",
        name: "student",
        component: () => import("@/views/Student"),
        meta: { title: "学生管理", icon: "icon-xueshengguanli" }
      }
    ]
  },

  {
    path: "/menu",
    component: Layout,
    children: [
      {
        path: "index",
        name: "menu",
        component: () => import("@/views/Menu"),
        meta: { title: "菜单管理", icon: "icon-caidanliebiao" }
      }
    ]
  },
  {
    path: "/curriculum",
    component: Layout,
    children: [
      {
        path: "index",
        name: "curriculum",
        component: () => import("@/views/Curriculum"),
        meta: { title: "课程管理", icon: "icon-kechengguanli" }
      }
    ]
  },
  {
    path: "/audit",
    component: Layout,
    children: [
      {
        path: "index",
        name: "audit",
        component: () => import("@/views/Audit"),
        meta: { title: "审核管理", icon: "icon-shenheguanli" }
      }
    ]
  },
  {
    path: "/news",
    component: Layout,
    children: [
      {
        path: "index",
        name: "news",
        component: () => import("../views/News"),
        meta: { title: "日志管理", icon: "icon-rizhiguanli" }
      }
    ]
  },
  {
    path: "/journal",
    component: Layout,
    children: [
      {
        path: "index",
        name: "journal",
        component: () => import("../views/Journal"),
        meta: { title: "消息管理", icon: "icon-xiaoxiguanlix" }
      }
    ]
  }
]

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
})
const routerPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return routerPush.call(this, location).catch(error => error)
}
router.beforeEach((to, from, next) => {
  const token = getToken()
  if (to.name !== "Login" && !token) next({ name: "Login" })
  else next()
})

export default router
